package org.example.mybatisdemo.mapper;

import org.apache.ibatis.annotations.*;
import org.example.mybatisdemo.model.UserInfo;

import java.util.List;

/**
 * 访问数据库
 */
@Mapper
public interface UserInfoMapper {
    /**
     * 返回数据列表
     *
     * @return
     */
    @Select("select  id, username, password, age, gender, phone, delete_flag as deleteFlag," +
            "create_time as createTime, update_time as updateTime from userinfo")
    List<UserInfo> queryUserList();

    @Results(id = "BaseResult", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("SELECT * FROM userinfo WHERE id = #{userId}")
    UserInfo queryUserInfo(Integer userId);

    @ResultMap(value = "BaseResult")
    @Select("SELECT * FROM userinfo WHERE id = #{userId} AND delete_flag = #{deleteFlag}")
    UserInfo queryUserInfoByDF(Integer userId, Integer deleteFlag);

    @Select("SELECT * FROM userinfo WHERE id = #{id} AND delete_flag = #{deleteFlag}")
    UserInfo queryUserInfoParam(@Param("id") Integer userId, Integer deleteFlag);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo(username, password, age, gender, phone)" +
            " values(#{username}, #{password}, #{age}, #{gender}, #{phone   })")
    Integer insert(UserInfo userInfo);

    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

    @Update("update userinfo set password = #{password} where id = #{id}")
    Integer update(String password, Integer id);

    @Update("update userinfo set username = #{username}, password = #{password}, age = #{age} where id = #{id}")
    Integer updateByOb(UserInfo userInfo);

    @Select("select * from userinfo order by id ${order}")
    List<UserInfo> queryUserByOrder(String order);
}
